package ch.ethz.fcl.metrobuzz.render.accessbility.view.global;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import org.newdawn.slick.util.ResourceLoader;

import ch.ethz.fcl.metrobuzz.spatialindex.object2d.Rectangle;

/**
 * accessibility values read from and write into files
 * 
 * @author ZengWei
 * 
 */
public class AccessibilityReaderWriter {
	public static AccessibilityMapCell[][] readAcceCells(String fileName) {
		AccessibilityMapCell[][] cells = null;

		try {
			InputStreamReader isr = new InputStreamReader(
					ResourceLoader.getResourceAsStream(fileName));
			BufferedReader br = new BufferedReader(isr);
			String s = br.readLine(); // first line

			String[] s1 = s.split("\t");

			// read dimensions
			String[] dimensions = s1[0].split(":");
			int xDim = Integer.parseInt(dimensions[1]);
			int yDim = Integer.parseInt(dimensions[2]);
			cells = new AccessibilityMapCell[xDim][yDim];

			// read minimum coordinates
			String[] coords = s1[1].split(":");
			float minX = Float.parseFloat(coords[1]);
			float minY = Float.parseFloat(coords[2]);

			// read length
			String[] lengths = s1[2].split(":");
			float length = Float.parseFloat(lengths[1]);

			br.readLine();// ignore second line

			int curLineNum = 0;
			while ((s = br.readLine()) != null) {
				String[] values = s.split(":");

				float curX = minX + curLineNum * length;
				for (int i = 0; i < values.length; i++) {
					float curY = minY + i * length;
					Rectangle area = new Rectangle(curX, curY, curX + length,
							curY + length);
					cells[curLineNum][i] = new AccessibilityMapCell(area);

					int v = Integer.parseInt(values[i]);

					if (v == -1)
						cells[curLineNum][i].setAreaInSg(false);
					else if (v == 1)
						cells[curLineNum][i].setAreaInSg(true);
				}

				curLineNum++;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return cells;
	}

	public static void writeAcceCells(String fileName,
			AccessibilityMapCell[][] cells) {
		try {
			FileWriter fw = new FileWriter(fileName);
			Rectangle area = cells[0][0].getArea();
			fw.write("Dimension:" + cells.length + ":" + cells[0].length + "\t"
					+ "Coord:" + area.getMinX() + ":" + area.getMinY() + "\t"
					+ "Length:" + area.getWidth() + "\n");
			fw.write("#These values indicate whether the cell is in SG (1 in, -1 not in)\n");
			for (int i = 0; i < cells.length; i++) {
				for (int j = 0; j < cells[0].length; j++) {
					AccessibilityMapCell cell = cells[i][j];
					int value = cell.isAreaInSg() ? 1 : -1;
					fw.write(value + ":");
				}

				fw.write("\n");
			}

			fw.flush();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void readAcceValues(String fileName,
			AccessibilityMapCell[][] cells) {
		try {
			InputStreamReader isr = new InputStreamReader(
					ResourceLoader.getResourceAsStream(fileName));
			BufferedReader br = new BufferedReader(isr);
			String s = br.readLine(); // ignore the first line

			int curLineNum = 0;
			while ((s = br.readLine()) != null) {
				String[] values = s.split("\t");

				for (int i = 0; i < values.length; i++)
					cells[curLineNum][i].setAcceValue(Float
							.parseFloat(values[i]));

				curLineNum++;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void writeIntoFile(String fileName,
			AccessibilityMapCell[][] cells, String info) {
		try {
			FileWriter fw = new FileWriter(fileName);
			fw.write(info + "\n");
			for (int i = 0; i < cells.length; i++) {
				for (int j = 0; j < cells[0].length; j++) {
					AccessibilityMapCell cell = cells[i][j];

					fw.write(cell.getAcceValue() + "\t");
				}

				fw.write("\n");
			}

			fw.flush();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
